from math import fabs

class case:
    def __init__(self, nationality, quality, price, music, area, open, solution = ""):
        self.nationality = nationality
        self.quality = quality
        self.price = price
        self.music = music
        self.area = area
        self.openHours = open
        self.solution = solution
    
class caseBase:
    def __init__(self):
        self.restaurantList = list()
        self.restaurantList.append(case("swedish", 4,3,3, "stortorget", {"mo", "tu", "we", "th", "fr", "sa", "su"},"Jensens Bofhus"))
        self.restaurantList.append(case("american", 3,2,0, {"vaghustorget", "soder"}, {"mo", "tu", "we", "th", "fr", "sa", "su"},"McDonalds"))
        self.restaurantList.append(case("asian", 3,3,3, "jarntorget", {"mo", "tu", "we", "th", "fr", "sa", "su"},"Coco-Thai"))
        self.restaurantList.append(case("swedish", 5,5,3, "stortorget", {"fr", "sa", "su"},"Slottskallaren"))
        self.restaurantList.append(case("greek", 4,5,1, "jarntorget", {"mo", "tu", "we", "th", "fr", "sa", "su"},"Kolgrillsbaren"))

    def findMatch(self, testCase):
        (bestCase, score) = (self.restaurantList[0], 0)
        for c in self.restaurantList:
            sc = self.similarity(testCase, c)
            if sc > score:
                (bestCase, score) = (c, sc)
        print(bestCase.solution)
    
    def nationalityCompare(self, testCase, c):
        if c.nationality == testCase.nationality:
            score = 1
        else:
            if testCase.nationality == "swedish":
                if c.nationality == "american":
                    score = 0.8
                elif c.nationality == "asian":
                    score = 0.4
                elif c.nationality == "greek":
                    score = 0.6


            elif testCase.nationality == "american":
                if c.nationality == "swedish":
                    score = 0.8
                elif c.nationality == "asian":
                    score = 0.5
                elif c.nationality == "greek":
                    score = 0.5


            elif testCase.nationality == "asian":
                if c.nationality == "swedish":
                    score = 0.4
                elif c.nationality == "american":
                    score = 0.5
                elif c.nationality == "greek":
                    score = 0.5


            elif testCase.nationality == "greek":
                if c.nationality == "swedish":
                    score = 0.6
                elif c.nationality == "american":
                    score = 0.5
                elif c.nationality == "asian":
                    score = 0.5
        return score

    def similarity(self, testCase, c):
        similarityMax = 40;
        nationalityValue = self.nationalityCompare(testCase, c)
        qualityValue =  1 - fabs(testCase.quality - c.quality)/5
        priceValue = 1 - fabs(testCase.price - c.price)/5
        musicValue = 1 - fabs(testCase.music - c.music)/5
        distanceValue = self.distanceCompare(testCase, c)
        openValue = self.openCompare(testCase, c)
        similarityScore = (1/similarityMax) * (nationalityValue * 5 + qualityValue * 5 + priceValue * 5 + musicValue * 5 + distanceValue * 5 + openValue * 15)
        return similarityScore

    def openCompare(self, testCase, c):
        for d in c.openHours:
            for d2 in testCase.openHours:
                if (d==d2):
                    return 1
        return 0
    def distanceCompare(self, testCase, c):
        best = 0
        candidate = 0
        for loc in c.area:
            for tloc in testCase.area:
                if tloc == loc:
                    return 1
                else:
                    if tloc == "jarntorget":
                        if loc == "stortorget":
                            candidate = 0.7
                        elif loc == "vaghustorget":
                            candidate = 0.5
                        elif loc == "soder":
                            candidate = 0.3
                    if tloc == "stortorget":
                        if loc == "jarntorget":
                            candidate = 0.7
                        elif loc == "vaghustorget":
                            candidate = 0.7
                        elif loc == "soder":
                            candidate = 0.5
                    if tloc == "vaghustorget":
                        if loc == "jarntorget":
                            candidate = 0.5
                        elif loc == "stortorget":
                            candidate = 0.7
                        elif loc == "soder":
                            candidate = 0.7
                    if tloc == "soder":
                        if loc == "jarntorget":
                            candidate = 0.3
                        elif loc == "stortorget":
                            candidate = 0.5
                        elif loc == "vaghustorget":
                            candidate = 0.7
                if candidate > best:
                    best = candidate
        return best


       

c = caseBase()
testCase = case("asian", 4,2,3,"jarntorget","mo")
c.findMatch(testCase)
testCase = case("swedish", 4,1,5,"soder","mo")
c.findMatch(testCase)


